package com.bonc.excel.biz.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.bonc.excel.dto.request.RequestDto;
import com.bonc.excel.entity.CateCnfInfoDto;
import com.bonc.excel.entity.ColDto;
import com.bonc.excel.entity.DataDto;
import com.bonc.excel.entity.RowDto;
import com.bonc.excel.service.CateCnfInfoService;
import com.bonc.excel.service.ColService;
import com.bonc.excel.service.DataService;
import com.bonc.excel.service.RowService;
import com.bonc.excel.biz.BaseExcelService;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author: wujing
 * @Description: 默认
 **/

@Service
@Slf4j
public class defaultServiceImpl extends BaseExcelService {

    @Autowired
    ColService colService;

    @Autowired
    CateCnfInfoService cateCnfInfoService;

    @Autowired
    DataService dataService;

    @Autowired
    RowService rowService;

    @Override
    public Map<String, List> getData(RequestDto req)   {
        checkMonthId(req);
        String categroyCode = req.getCode();
        //分类表
        List<CateCnfInfoDto> cateInfoList = cateCnfInfoService.lambdaQuery()
                .eq(StringUtils.isNotBlank(categroyCode),CateCnfInfoDto::getCategroy,categroyCode).list();

        //列表头
        List<ColDto> colList = colService.lambdaQuery()
                .eq(StringUtils.isNotBlank(categroyCode),ColDto::getCategroyCode,categroyCode).list();

        //row表头
        List<RowDto> rowList = rowService.lambdaQuery()
                .eq(StringUtils.isNotBlank(categroyCode),RowDto::getCategroyCode,categroyCode).list();
        //数据
        List<DataDto> dataList = dataService.lambdaQuery()
                .eq(StringUtils.isNotBlank(categroyCode),DataDto::getCategroyCode,categroyCode)
              //  .eq(StringUtils.isNotBlank(req.getMonthId()),DataDto::getMonthId,req.getMonthId())
              //  .eq(StringUtils.isNotBlank(req.getDayId()),DataDto::getDayId,req.getDayId())
                .list();

        if(CollectionUtil.isEmpty(cateInfoList)
                || CollectionUtil.isEmpty(colList)
                ||CollectionUtil.isEmpty(rowList)
                ||CollectionUtil.isEmpty(dataList)){
            log.info("数据未配置");
            return Collections.emptyMap();
        }
        Map<String, List<ColDto>> colMap = colList.stream().collect(Collectors.groupingBy(ColDto::getCategroyCode));
        Map<String, List<RowDto>> rowMap = rowList.stream().collect(Collectors.groupingBy(RowDto::getCategroyCode));
        Map<String, List<DataDto>> dataMap = dataList.stream().collect(Collectors.groupingBy(DataDto::getCategroyCode));
        Map<String, CateCnfInfoDto> cateInfoMap = cateInfoList.stream()
                .collect(Collectors.toMap(CateCnfInfoDto::getCategroyCode, Function.identity(),(k1, k2) -> k1));
        Map<String, List> resultMap = Maps.newLinkedHashMap();
        cateInfoMap.forEach((code,info)->{
            try {
                //  CtClass ctClass = child.get("com.bonc.dto.response.CaseDetailRspVo");
                List<String> datas = dataMap.get(code).stream().map(list -> list.getValue1()).collect(Collectors.toList());
                List<ColDto> colDtos = colMap.get(code);
                List<String> rowHeads = rowMap.get(code).stream()
                        .sorted(Comparator.comparing(RowDto::getRowOrder))
                        .map(list -> list.getRowHead()).collect(Collectors.toList());
                List<Object> objects = setData(code, /*child,*/ datas,rowHeads, colDtos);
                //resultMap.put(code,objects);
                resultMap.put(info.getName(),objects);
            } catch (Exception e) {
                log.error("handle cateCode:{} error",code,e);
            }
        });

        return resultMap;

    }

    private void checkMonthId(RequestDto req) {
        LocalDateTime lastDateTime = LocalDate.now().minusDays(1).atStartOfDay();
        String nowDateStr = DateUtil.format(lastDateTime, "yyyyMMdd");
        if (StringUtils.isBlank(req.getMonthId())){
            req.setMonthId(nowDateStr.substring(0, 6));
            req.setDayId(nowDateStr.substring(6, 8));
        }
    }


    public static void main(String[] args) {

        String content = "abc";
        String regex = "\\uE001.*?\\uE001";
        String replaced = content.replaceAll(regex, "");
        System.out.println(replaced);

        System.out.println(Base64.getEncoder().encodeToString("data_crypto".getBytes()));
    }
}
